/*
* Copyright (c) 2011 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
var serverPath = '//gibsonkanye.appspot.com/';

// Audio
var kanyeURL = 'http://gibsonkanye.appspot.com/static/kanye.wav';
var kanyeSound = gapi.hangout.av.effects.createAudioResource(kanyeURL).createSound();

function playClip() {
	kanyeSound.play({loop: false});
}

//Volume change event handling
var randomnumber;
gapi.hangout.av.onVolumesChanged.add(interrupt(gapi.hangout.av.VolumesChangedEvent));

function interrupt(myVolumesChangedEvent) {
	console.log('Volume changed');
	console.log(gapi.hangout.getParticipants())
	randomnumber=Math.floor(Math.random()*gapi.hangout.getParticipants().length);
	gapi.hangout.av.setParticipantAudioLevel(gapi.hangout.getParticipants()[randomnumber].id,0);
	playClip();
	gapi.hangout.av.setParticipantAudioLevel(gapi.hangout.getParticipants()[randomnumber].id,1);
}


// A function to be run at app initialization time which registers our callbacks
function init() {
  console.log('Init app.');

  var apiReady = function(eventObj) {
    if (eventObj.isApiReady) {
      console.log('API is ready');

      gapi.hangout.data.onStateChanged.add(function(eventObj) {
        updateStateUi(eventObj.state);
      });
      gapi.hangout.onParticipantsChanged.add(function(eventObj) {
        updateParticipantsUi(eventObj.participants);
      });

      updateStateUi(gapi.hangout.data.getState());
      updateParticipantsUi(gapi.hangout.getParticipants());

      gapi.hangout.onApiReady.remove(apiReady);
    }
  };

  // This application is pretty simple, but use this special api ready state
  // event if you would like to any more complex app setup.
  gapi.hangout.onApiReady.add(apiReady);
}


gadgets.util.registerOnLoadHandler(init);
